#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2017/3/4
import configparser

config = configparser.ConfigParser()  # 声明

config["DEFAULT"] = {  # 默认节点 在这里定义之后 每个节点下面都会有此节点
    "ServerAliveInterval": "45",  # 对应的子节点
    "Compression": "yes",
    "CompressionLevel": "9"
}

config["bitbucket.org"] = {}  # 新的空节点
config["bitbucket.org"]["user"] = "hg"  # 向空节点内添加内容
config["topsecret.server.com"] = {}
topsecret = config["topsecret.server.com"]
topsecret["Host Port"] = "50022"
topsecret["ForwardX11"] = "no"

config["DEFAULT"]["ForwardX11"] = "yes"
with open("example.ini", "w") as configfile:  # 存起来
    config.write(configfile)

# 读取

config = configparser.ConfigParser()

config.read("example.ini")

print(config.defaults())  # 读取default节点
print(config.sections())  # 读取其他节点

for key in config["bitbucket.org"]: print(key)  # 循环读取
